home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ditroff / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-10  |  12.4 KB  |  527 lines

  1. #ifndef DEFPRT
  2. #define DEFPRT "lw"
  3. #endif
  4.  
  5. #define TROFF     "/sprite/cmds.$MACHINE/troff_p"
  6. #define LPR    "/sprite/cmds.$MACHINE/lpr"
  7. #define DTBL     "/sprite/cmds.$MACHINE/tbl"
  8. #define DEQN     "/sprite/cmds.$MACHINE/eqn"
  9. #define GRN     "/sprite/cmds.$MACHINE/grn"
  10. #define PIC     "/sprite/cmds.$MACHINE/pic"
  11. #define REFER     "/sprite/cmds.$MACHINE/refer"
  12. #define IDEAL     "/sprite/cmds.$MACHINE/ideal"
  13.  
  14. #define TMPFILE "/tmp/ditXXXXX"
  15.  
  16. static char tempfile[] = TMPFILE;
  17.  
  18. char    *printer = DEFPRT;    /* printer to use */
  19. char    *ditentry;        /* ditcap entry to use */
  20. char    *type;            /* type of printer */
  21. char    *font;            /* font directory */
  22.  
  23. #include <stdio.h>
  24. #define    NULL    0
  25. #define BUFSIZ    1024
  26. char    ditline[BUFSIZ];    /* variables needed to read the ditcap lines*/
  27. char    ditbuf[BUFSIZ/2];    /*  and the ditcap file  */
  28. char    *bp;
  29.  
  30. #define BLANK ""
  31. char    files[BUFSIZ] = BLANK;    /* files to print  */
  32. char    flags[BUFSIZ/4] = BLANK;/* troff options - pass to TROFF */
  33. char    spool[BUFSIZ/4] = BLANK;/* spooling options - pass to LPR  */
  34. char    line1[BUFSIZ] = BLANK;    /* lines to be executed   */
  35. char    line2[BUFSIZ] = BLANK;
  36. char    line3[BUFSIZ] = BLANK;
  37. char    type_font[BUFSIZ/4] = BLANK;
  38. char    temp[BUFSIZ] = BLANK;    /* temporary buffer */
  39.  
  40. char    *FD;            /* ditcap characteristics  */
  41. char    *TR;
  42. char    *LO;
  43. char    *F1;
  44. char    *F2;
  45. char    *F3;
  46. char    *F4;
  47. char    *OL;
  48. char    *FA;
  49. char    *OA;
  50. char    *FT;
  51. char    *OT;
  52. char    *FP;
  53. char    *OP;
  54. char    *PV;
  55. char    *LP;
  56. char    *TB;
  57. char    *EQ;
  58. char    *GR;
  59. char    *PI;
  60. char    *RF;
  61. char    *ID;
  62. char    *SE;
  63.  
  64.  
  65. main(argc, argv)
  66. int argc;
  67. char **argv;
  68. {
  69.     char *cp;
  70.  
  71.     int     gottype=0, gotjob=0, gotfont=0, gotdit=0;
  72.     int     t=0, a=0;
  73.     int    tbl=0, eqn=0, grn=0, seqn=0, pic=0, refer=0, ideal=0, verbose=0;
  74.     int    first=1;
  75.     int    debug = 0;
  76.  
  77.     char     *operand();
  78.     char     *getenv();
  79.     char     *gettype();
  80.     char    *mktemp();
  81.     int    strlen();
  82.     int    strcmp();
  83.  
  84.     if (cp=getenv("PRINTER")) printer = cp;
  85.     if (cp=getenv("TYPESETTER")) printer = cp;
  86.     ditentry = printer;
  87.  
  88.     while (--argc) {
  89.         if (**++argv != '-')    /* if not an option add to files */
  90.         sprintf(files,"%s %s",files,*argv);
  91.         else
  92.           switch ((*argv)[1]) {      /* process options  */
  93.  
  94.         case 'P':    /* final output typesetter name */
  95.             printer = operand(&argc, &argv);
  96.             if (!gotdit) ditentry = printer;
  97.             break;
  98.  
  99.         case 'T':    /* printer type */
  100.             type = operand(&argc, &argv);
  101.             gottype = 1;
  102.             break;
  103.  
  104.         case 'F':    /* font directory */
  105.             font = operand(&argc, &argv);
  106.             gotfont = 1;
  107.             break;
  108.  
  109.         case 'D':    /* ditcap entry */
  110.             ditentry = operand(&argc, &argv);
  111.             gotdit = 1;
  112.             break;
  113.  
  114.         case 't':    
  115.             if ((*argv)[2]) { 
  116.                 if (strcmp(*argv,"-tbl") == 0) 
  117.                     tbl = 1;             /* -tbl option */
  118.                 else                /* some other option */
  119.                     sprintf(flags,"%s %s",flags,*argv);
  120.             } else {                      /* must be -t option  */
  121.                 sprintf(flags,"%s %s",flags,*argv);
  122.                 t = 1;
  123.             }
  124.             break;
  125.  
  126.         case 'a':    /* ascii approximation to standard output  */
  127.             if ((*argv)[2]) { 
  128.                 sprintf(flags,"%s %s",flags,*argv);
  129.             } else {                      /* must be -a option  */
  130.                 sprintf(flags,"%s %s",flags,*argv);
  131.                 a = 1;
  132.             }
  133.             break;
  134.  
  135.         case 'm':    /* either -mmac TROFF option or
  136.                       -m spooling option   */
  137.             if ((*argv)[2]) 
  138.                 sprintf(flags,"%s %s",flags,*argv);
  139.             else
  140.                 sprintf(spool,"%s %s",spool,*argv);
  141.             break;
  142.  
  143.         case 'h':    /* spooling options  */
  144.             if ((*argv)[2]) 
  145.                 sprintf(flags,"%s %s",flags,*argv);
  146.             else
  147.                 sprintf(spool,"%s %s",spool,*argv);
  148.             break;
  149.  
  150.         case '#':    /* spooling options  */
  151.             sprintf(spool,"%s %s",spool,*argv);
  152.             break;
  153.  
  154.         case 'C':    /* change class name - to spooling options */
  155.             cp = operand(&argc, &argv);
  156.             sprintf(temp," -C%s%s",cp,spool);
  157.             strcpy(spool,temp);
  158.             break;
  159.  
  160.         case 'J':    /* change job name - to spooling options */
  161.             cp = operand(&argc, &argv);
  162.             sprintf(temp," -J%s%s",cp,spool);
  163.             strcpy(spool,temp);
  164.             gotjob = 1;
  165.             break;
  166.  
  167.         case 'e':    
  168.             if (strcmp(*argv,"-eqn") == 0) 
  169.                 eqn = 1;             
  170.             else                
  171.                 sprintf(flags,"%s %s",flags,*argv);
  172.             break;
  173.  
  174.         case 'd':    
  175.             if (strcmp(*argv,"-deqn") == 0) 
  176.                 eqn = 1;             
  177.             else if (strcmp(*argv,"-dtbl") == 0) 
  178.                 tbl = 1;             
  179.             else if (strcmp(*argv,"-debug") == 0) 
  180.                 debug = 1;             
  181.             else
  182.                 sprintf(flags,"%s %s",flags,*argv);
  183.             break;
  184.  
  185.         case 's':    
  186.             if (strcmp(*argv,"-seqn") == 0) {
  187.                 eqn = 1;             
  188.                 seqn = 1;             
  189.             } else                
  190.                 sprintf(flags,"%s %s",flags,*argv);
  191.             break;
  192.  
  193.         case 'g':    
  194.             if (strcmp(*argv,"-grn") == 0) 
  195.                 grn = 1;             
  196.             else                
  197.                 sprintf(flags,"%s %s",flags,*argv);
  198.             break;
  199.  
  200.         case 'p':    
  201.             if (strcmp(*argv,"-pic") == 0) 
  202.                 pic = 1;             
  203.             else                
  204.                 sprintf(flags,"%s %s",flags,*argv);
  205.             break;
  206.  
  207.         case 'r':    
  208.             if (strcmp(*argv,"-refer") == 0) 
  209.                 refer = 1;             
  210.             else                
  211.                 sprintf(flags,"%s %s",flags,*argv);
  212.             break;
  213.  
  214.         case 'v':
  215.             verbose = 1;
  216.             break;
  217.             
  218.         case 'i':    
  219.             if (strcmp(*argv,"-ideal") == 0) 
  220.                 ideal = 1;             
  221.             else                
  222.                 sprintf(flags,"%s %s",flags,*argv);
  223.             break;
  224.  
  225. /* here's the bug: if argument is just '-', it IS a file, i.e. stdin */
  226.         case '\0':
  227.             sprintf(files,"%s %s",files,*argv);
  228.             break;
  229. /* end of bug fix [as, 5/25/89] */
  230.  
  231.         default:    /* option to be passed to TROFF  */
  232.             sprintf(flags,"%s %s",flags,*argv);
  233.         }
  234.     }
  235.       
  236.     
  237.     if (!gotjob) {        /* make sure there is a jobname  */
  238.         sprintf(temp," -Jditroff%s",spool);
  239.         strcpy(spool,temp);
  240.     }
  241.  
  242.                 /* get the ditcap line from the ditcap  */
  243.                 /* file or environment variable  */
  244.     getditline(ditentry);
  245.  
  246.                 /* get the printer type for the given   */
  247.                 /*  ditcap entry from the ditcap file   */
  248.     if (!gottype)  type = gettype(ditentry); 
  249.                 /* get the options in the ditcap line   */
  250.     ditoptions();
  251.  
  252.  
  253.                 /* start building up the lines that we */
  254.                 /* will pass to UNIX to be executed    */
  255.                 /* according to */
  256.                 /* options and then execute the lines */
  257.  
  258.     if (seqn) {
  259.         if (SE == NULL) {
  260.              fprintf (stderr,"ditroff: se characteristic not defined in the ditcap file \n");
  261.              fprintf (stderr,"          no special equation characters defined\n");
  262.         } else if (strlen(files) == 0) {
  263.             fprintf (stderr,"ditroff: warning: ");
  264.             fprintf (stderr,"you can't use the -seqn option ");
  265.             fprintf (stderr,"when ditroff \n");
  266.             fprintf (stderr,"                  ");
  267.             fprintf (stderr,"is not given files as arguements \n");
  268.             fprintf (stderr,"                  ");
  269.             fprintf (stderr,"(ie. when the input is to come");
  270.             fprintf (stderr," from standard input) \n");
  271.         } else {
  272.              sprintf(temp," %s%s",SE,files);
  273.              strcpy(files,temp);
  274.         }
  275.     }
  276.  
  277.     sprintf(type_font," -T%s",type);
  278.     if (gotfont)
  279.         sprintf(type_font,"%s -F%s",type_font,font);
  280.     else if (FD != NULL)
  281.         sprintf(type_font,"%s -F%s",type_font,FD);
  282.  
  283.     if (refer) {
  284.         sprintf(line1,"%s%s|",RF,files);
  285.         first = 0;
  286.     }
  287.     if (tbl){
  288.         if (first)
  289.             sprintf(line1,"%s%s|",TB,files);
  290.         else
  291.             sprintf(line1,"%s%s|",line1,TB);
  292.         first = 0;
  293.     }
  294.     if (grn){
  295.         if (first)
  296.             sprintf(line1,"%s%s%s|",GR,type_font,files);
  297.         else
  298.             sprintf(line1,"%s%s%s|",line1,GR,type_font);
  299.         first = 0;
  300.     }
  301.     if (pic){
  302.         if (first)
  303.             sprintf(line1,"%s%s%s|",PI,type_font,files);
  304.         else
  305.             sprintf(line1,"%s%s%s|",line1,PI,type_font);
  306.         first = 0;
  307.     }
  308.     if (ideal){
  309.         if (first)
  310.             sprintf(line1,"%s%s%s|",ID,type_font,files);
  311.         else
  312.             sprintf(line1,"%s%s%s|",line1,ID,type_font);
  313.         first = 0;
  314.     }
  315.     if (eqn){
  316.         if (first)
  317.             sprintf(line1,"%s%s%s|",EQ,type_font,files);
  318.         else
  319.             sprintf(line1,"%s%s%s|",line1,EQ,type_font);
  320.         first = 0;
  321.     }
  322.  
  323.  
  324.                 /* add the end of the line depending  */
  325.                 /* on the options                     */
  326.  
  327.     if (a) {        /* ascii approximation   */
  328.         sprintf(line1,"%s%s%s",line1,TR,type_font);
  329.         if (OA != NULL) sprintf(line1,"%s %s",line1,OA);
  330.         sprintf(line1,"%s%s",line1,flags);
  331.         if (first) sprintf(line1,"%s%s",line1,files);
  332.  
  333.         if (FA != NULL) sprintf(line1,"%s|%s",line1,FA);
  334.  
  335.         if (debug)
  336.                 printf("line1 is \n  %s \n",line1);
  337.         else
  338.             system(line1);
  339.  
  340.     }
  341.     else if (t) {          /* send to standard output  */
  342.         sprintf(line1,"%s%s%s",line1,TR,type_font);
  343.         if (OT != NULL) sprintf(line1,"%s %s",line1,OT);
  344.         sprintf(line1,"%s%s",line1,flags);
  345.         if (first) sprintf(line1,"%s%s",line1,files);
  346.  
  347.         if (FT != NULL) sprintf(line1,"%s|%s",line1,FT);
  348.  
  349.         if (debug)
  350.                 printf("line1 is \n  %s \n",line1);
  351.         else
  352.             system(line1);
  353.  
  354.     }
  355.     else if (PV != NULL) {    /* use the previewer  */
  356.         cp = mktemp(tempfile);
  357.         strcpy(temp,cp);
  358.  
  359.         sprintf(line1,"%s%s%s",line1,TR,type_font);
  360.         if (OP != NULL) sprintf(line1,"%s %s",line1,OP);
  361.         sprintf(line1,"%s%s",line1,flags);
  362.         if (first) sprintf(line1,"%s%s",line1,files);
  363.  
  364.         if (FP != NULL) sprintf(line1,"%s|%s",line1,FP);
  365.  
  366.         sprintf(line1,"%s > %s",line1,temp);
  367.         sprintf(line2,"%s %s",PV,temp);
  368.         sprintf(line3,"/sprite/cmds.$MACHINE/rm -f %s",temp);
  369.  
  370.         if (debug) {
  371.                 printf("line1 is \n  %s \n",line1);
  372.                 printf("line2 is \n  %s \n",line2);
  373.                 printf("line3 is \n  %s \n",line3); 
  374.         } else {
  375.             system(line1);
  376.             system(line2); 
  377.             system(line3);
  378.         }
  379.  
  380.     }
  381.     else {             /* standard troff to printer  */
  382.         sprintf(line1,"%s%s%s",line1,TR,type_font);
  383.         if (OL != NULL) sprintf(line1,"%s %s",line1,OL);
  384.         sprintf(line1,"%s%s",line1,flags);
  385.         if (first) sprintf(line1,"%s%s",line1,files);
  386.  
  387.         if (F1 != NULL) sprintf(line1,"%s|%s",line1,F1);
  388.         if (F2 != NULL) sprintf(line1,"%s|%s",line1,F2);
  389.         if (F3 != NULL) sprintf(line1,"%s|%s",line1,F3);
  390.         if (F4 != NULL) sprintf(line1,"%s|%s",line1,F4);
  391.  
  392.         sprintf(line1,"%s|%s",line1,LP);
  393.         sprintf(line1,"%s -P%s",line1,printer);
  394.         sprintf(line1,"%s%s",line1,spool);
  395.         if (LO != NULL) sprintf(line1,"%s %s",line1,LO);
  396.  
  397.         if (debug) 
  398.                 printf("line1 is \n  %s \n",line1);
  399.         else 
  400.             system(line1);
  401.  
  402.     }
  403.  
  404.  
  405. }
  406.  
  407.  
  408. /*----------------------------------------------------------------------------*
  409.  | Routine:    char  * operand (& argc,  & argv)
  410.  |
  411.  | Results:    returns address of the operand given with a command-line
  412.  |        option.  It uses either "-Xoperand" or "-X operand", whichever
  413.  |        is present.  The program is terminated if no option is present.
  414.  |
  415.  | Side Efct:    argc and argv are updated as necessary.
  416.  *----------------------------------------------------------------------------*/
  417.  
  418. char *operand(argcp, argvp)
  419. int * argcp;
  420. char ***argvp;
  421. {
  422.     if ((**argvp)[2]) return(**argvp + 2); /* operand immediately follows */
  423.     if ((--*argcp) <= 0) {            /* no operand */
  424.         fprintf(stderr,"ditroff: command-line option operand missing.\n");
  425.         exit(8);
  426.     }
  427.     return(*(++(*argvp)));            /* operand is next word */
  428. }
  429.  
  430.  
  431. char     *dgetstr();
  432.  
  433. /*----------------------------------------------------------------------------*
  434.  | Routine:    getditline(printer)
  435.  |
  436.  | Results:    gets the ditcap line for the printer specified
  437.  *----------------------------------------------------------------------------*/
  438.  
  439. getditline(device)
  440.     char    *device;
  441. {
  442.     int     status;
  443.  
  444.     if ((status = dgetent(ditline,device)) < 0) {
  445.          fprintf(stderr,"ditroff: can't open the ditcap file \n");
  446.          exit(1);
  447.     }
  448.     else if (status == 0) {
  449.          fprintf(stderr,"ditroff: can't find device %s in the ditcap file \n", device);
  450.          exit(1);
  451.     }
  452.     bp = ditbuf;
  453. }
  454.  
  455. /*----------------------------------------------------------------------------*
  456.  | Routine:    char *gettype (printer)
  457.  |
  458.  | Results:    returns the printer type by looking at the ditcap line
  459.  *----------------------------------------------------------------------------*/
  460.  
  461. char *
  462. gettype(device)
  463.     char    *device;
  464. {
  465.     char     *cp;
  466.  
  467.     if ((cp = dgetstr("ty", &bp)) == NULL) {
  468.          fprintf(stderr,"ditroff: no type specified for device %s in the ditcap file \n", device);
  469.          exit(1);
  470.     }
  471.     return (cp);
  472. }
  473.  
  474.  
  475. /*----------------------------------------------------------------------------*
  476.  | Routine:    ditoptions()
  477.  |
  478.  | Results:    gets the options from the ditcap line
  479.  *----------------------------------------------------------------------------*/
  480.  
  481. ditoptions()
  482. {
  483.     char *cp;
  484.  
  485.     FD = dgetstr("fd",&bp);
  486.  
  487.     TR = TROFF;
  488.     if (cp=dgetstr("tr",&bp)) TR = cp; 
  489.  
  490.     LO = dgetstr("lo",&bp);
  491.     F1 = dgetstr("f1",&bp);
  492.     F2 = dgetstr("f2",&bp);
  493.     F3 = dgetstr("f3",&bp);
  494.     F4 = dgetstr("f4",&bp);
  495.     OL = dgetstr("ol",&bp);
  496.     FA = dgetstr("fa",&bp);
  497.     OA = dgetstr("oa",&bp);
  498.     FT = dgetstr("ft",&bp);
  499.     OT = dgetstr("ot",&bp);
  500.     FP = dgetstr("fp",&bp);
  501.     OP = dgetstr("op",&bp);
  502.     PV = dgetstr("pv",&bp);
  503.  
  504.     LP = LPR;
  505.     if (cp=dgetstr("lp",&bp)) LP = cp; 
  506.  
  507.     TB = DTBL;
  508.     if (cp=dgetstr("tb",&bp)) TB = cp; 
  509.  
  510.     EQ = DEQN;
  511.     if (cp=dgetstr("eq",&bp)) EQ = cp; 
  512.  
  513.     GR = GRN;
  514.     if (cp=dgetstr("gr",&bp)) GR = cp; 
  515.  
  516.     PI = PIC;
  517.     if (cp=dgetstr("pi",&bp)) PI = cp; 
  518.  
  519.     RF = REFER;
  520.     if (cp=dgetstr("rf",&bp)) RF = cp; 
  521.  
  522.     ID = IDEAL;
  523.     if (cp=dgetstr("id",&bp)) ID = cp; 
  524.  
  525.     SE = dgetstr("se",&bp);
  526. }
  527.